package com.tuo51.foudation.utils;

import com.tuo51.core.domain.org.Organization;
import org.joda.time.DateTime;
import org.joda.time.Days;

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

public class MemberUtils {



    /**
     * 当前是否是会员
     * @param user
     * @return
     */
//    @Deprecated
//    public static boolean isVIP(Staff user){
//
//        if(user == null
//                || user.getUserVip() == null
//                || user.getUserVip().getExpireTime() == null
//                || user.getUserVip().getVipLevel() == null
//                || user.getUserVip().getVipLevel().intValue() == 0)
//
//            return false;
//
//        Calendar calendar = Calendar.getInstance();
//        Date now = calendar.getTime();
//
//        if(user.getUserVip().getExpireTime().before(now))
//            return false;
//
//        return true;
//    }

    public static boolean isVIP(Organization org){

        if(org == null
                || org.getExpireTime() == null )
            return false;

        Calendar calendar = Calendar.getInstance();
        Date now = calendar.getTime();

        if(org.getExpireTime().before(now))
            return false;

        return true;
    }

//    @Deprecated
//    public static void setUserVIPTimeOut(Staff user){
//        if(user == null || user.getUserVip() == null) return;
//        user.getUserVip().setVipLevel(APPConstants.Member.REGISTER_MEMBER.getValue());
//        user.getUserVip().setExpireTime(null);
//    }

    public static void setUserVIPTimeOut(Organization org){
        if(org == null || org.getExpireTime() == null) return;
        org.setExpireTime(null);
    }

//    @Deprecated
//    public static void addUserVIPDuration(Staff user , int duration , APPConstants.Member member){
//
//        if(duration <= 0) return;
//
//        Calendar calendar = Calendar.getInstance();
//
//        StaffVip vip = user.getUserVip();
//        vip.setVipLevel(member.getValue());
//
//        Date now = new Date();
//        if(Objects.nonNull(vip.getExpireTime()) &&  now.compareTo(vip.getExpireTime()) == -1) {
//            calendar.setTime(user.getUserVip().getExpireTime());
//            calendar.add(Calendar.SECOND, duration);
//
//            vip.setExpireTime(calendar.getTime());
//        }else{
//            calendar.setTime(now);
//            calendar.add(Calendar.SECOND, duration);
//            vip.setExpireTime(calendar.getTime());
//        }
//    }

//    @Deprecated
//    public static void addUserVIPTimes(Staff user , int times , APPConstants.Member member){
//
//        if(times <= 0) return;
//
//        Calendar calendar = Calendar.getInstance();
//
//        boolean isVIP = isVIP(user);
//
//        StaffVip userVip = user.getUserVip();
//        if(userVip.getRestCount() == null || userVip.getRestCount() <= 0){
//            userVip.setRestCount(times);
//        }else{
//            userVip.setRestCount(times + userVip.getRestCount());
//        }
//
//        if(userVip.getVipLevel()  == null || userVip.getVipLevel() == APPConstants.Member.REGISTER_MEMBER.getValue()){
//            userVip.setVipLevel(APPConstants.Member.TIMES_MEMBER.getValue());
//            userVip.setVipDesc(APPConstants.Member.TIMES_MEMBER.getDesc());
//        }
//    }

    public static void addUserVIPDuration(Organization org , int duration){

        if(duration <= 0) return;

        Calendar calendar = Calendar.getInstance();

        Date now = new Date();

        if(Objects.nonNull(org.getExpireTime()) &&  now.compareTo(org.getExpireTime()) == -1) {
            calendar.setTime(org.getExpireTime());
            calendar.add(Calendar.SECOND, duration);

            org.setExpireTime(calendar.getTime());
        }else{
            calendar.setTime(now);
            calendar.add(Calendar.SECOND, duration);
            org.setExpireTime(calendar.getTime());
        }

    }

    public static void addUserVIPTimes(Organization org, int times){
        if(times <= 0) return;

        if(org.getRestCount() == null || org.getRestCount() <= 0){
            org.setRestCount(times);
        }else{
            org.setRestCount(times + org.getRestCount());
        }
    }

    /**
     * 计算购买的VIP 天数,不足一天以一天计算
     * @param purchaseDateMs 购买时间
     * @param expiresDateMs 失效时间
     * @return
     */
    public static int countVipDays(long purchaseDateMs,long expiresDateMs){

        Date expiresDate = new Date(expiresDateMs);
        Date purchaseDate = new Date(purchaseDateMs);
        Calendar calendar = Calendar.getInstance();
        Date nowDate = calendar.getTime();
        int days = caculate2Days(purchaseDate,expiresDate);
        if(days == 0 && expiresDate.after(nowDate)) days = 1;

        return days;
    }

    public static int caculate2Days(Date firstDate, Date secondDate) {

        if(firstDate == null || secondDate == null) return 0;
        DateTime nowTime = new DateTime(firstDate);
        DateTime futureTime = new DateTime(secondDate);

        int days = Days.daysBetween(nowTime, futureTime).getDays();
        return days;
    }

}
